﻿@{
    ViewData["Title"] = "AddView";
    Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="">
<div class="layui-form-item">
    <label class="layui-form-label">账号</label>
    <div class="layui-input-block">
            <input type="text" id="accountName" name="accountName" autocomplete="off" placeholder="请输入账号" lay-verify="required" class="layui-input">
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">密码</label>
    <div class="layui-input-block">
            <input type="text" id="accountPass" name="accountPass" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">昵称</label>
    <div class="layui-input-block">
            <input type="text" id="nickname" name="nickname" lay-verify="required" placeholder="请输入昵称" autocomplete="off" class="layui-input">
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">手机号</label>
    <div class="layui-input-block">
            <input type="text" id="phone" name="phone" lay-verify="required" placeholder="请输入手机号" autocomplete="off" class="layui-input">
    </div>
</div>



<div class="layui-form-item">
    <label class="layui-form-label">角色</label>
    <div class="layui-input-inline">
            <select name="roleName" id="roleName" lay-filter="aihao">
                <option value="">请选择角色</option>
            </select>
    </div>
</div>



<div class="layui-form-item">
    <label class="layui-form-label">是否启用</label>
    <div class="layui-input-block">
        <input type="radio" id="isNo" name="isNo" value="1" title="是" checked>
        <input type="radio" id="isNo" name="isNo" value="0" title="否">
    </div>
</div>


<div class="layui-form-item">
    <div class="layui-input-block">
        <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
    </div>
</div>
</form>

<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>

<script>
       // 页面加载完成后执行的函数
    $(function () {
        getMainUnitList()
    });


    //获取角色列表
    function getMainUnitList() {

        $.ajax({
            url: '/Account/Select',
            type: 'get',
            dataType: 'json',
             async:false,
            success: function (res) {
            console.log(res.data);
                var select = $('#roleName'); // 获取 select 元素

                // 清空现有的选项（除了第一个默认选项）
                select.find('option:not(:first)').remove();
                console.log(res.data);
                // 动态添加选项
                $.each(res.data, function (key, value) {
                    select.append('<option value="' + value.roleName + '">' + value.roleName + '</option>');
                });

                layui.form.render('select');

            }
        })

    }



    layui.use(['form', 'laydate', 'util'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var util = layui.util;

      // 提交事件
      form.on('submit(demo1)', function(data){
          console.log(data);
          var field = data.field; // 获取表单字段值

          if(field.roleName==""){
               layer.msg('请选择角色');
               return
          }

          //手机号验证
          if (!/^1[3-9]\d{9}$/.test($("#phone").val())) {
              layer.msg('请输入正规手机号');
              return ;
          }

          $.ajax({
              url:'/Account/Add',
              type:'post',
              data:field,
              dataType:'json',
              success:function(res){
                  console.log(res);
                  if(res>0){
                       layer.msg('添加成功');
                       setTimeout(() => {
                           closeWorkstation();
                       }, 2000);
                  }else{
                       layer.msg('添加失败账号重复');
                       return
                  }
              }
          })
          return false; // 阻止默认 form 跳转
      });


    });


    //关闭弹框
    function closeWorkstation() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前的索引
        parent.layer.close(index); //再执行关闭
    }

</script>

